<script setup lang="ts">
	import { onLaunch, onShow, onHide } from '@dcloudio/uni-app';
	import { usePlatformStore,useStatusStore,useWebConfigStore,useUserStore } from "@/stores";
	import { useStorage,$helper,$api } from "@/utils";
	let watchShowGetWebConfig = false;
	onLaunch((options:any) => {
		console.log('App Launch')
		const PlatformStore = usePlatformStore();
		PlatformStore.checkPlatform();
		const storeage = useStorage();
		const appid = options.query?.appid || $helper.appid();
		if (appid) {
			// #ifdef H5
			storeage.set('APPID', appid);
			// #endif
		} else {
			uni.showModal({
				content: '授权地址不正确，请重新访问',
				showCancel: false
				, success() {
					// #ifdef H5
					globalThis.close();
					// #endif
					// #ifdef MP-WEIXIN
					uni.navigateBack();
					// #endif
				}
			})
		}
		const StatusStore=useStatusStore();
		// 初始化全局状态
		StatusStore.init();
		$api.Ads.ads('tabbar').then((res:any) => {
			StatusStore.set('tabbar',res)
		}).catch(() => {
			StatusStore.resetTabbar()
		})
		// 初始化网站配置
		const WebConfigStore=useWebConfigStore();
		WebConfigStore.initWebConfig();
		$api.Config.webconfig().finally(() => {
			watchShowGetWebConfig = true;
		});
		// 初始化邀请码
		const icode = options.query?.icode;
		if (icode && !storeage.get('ICODE.' + appid)) {
			storeage.set('ICODE.' + appid, icode);
		}
		const puid = options.query?.puid;
		if (puid && !storeage.get('PUID.' + appid)) {
			storeage.set('PUID.' + appid, puid);
		}
		const dealer_uid = options.query?.dealer_uid;
		if (dealer_uid && !storeage.get('DEALER_UID.' + appid)) {
			storeage.set('DEALER_UID.' + appid, dealer_uid);
		}
		const channels_id=options.query?.channels_id;
		if(channels_id){
			const valid_time=Number(options.query?.valid_time)||0;
			storeage.set('CHANNELS_ID.' + appid, channels_id,valid_time*60*60);
		}
		// 初始化小程序场景码
		// #ifdef MP-WEIXIN
		const scene = options.query?.scene;
		if (scene) {
			const sceneArr = decodeURIComponent(scene).split('&');
			const sceneObj:any = {};
			sceneArr.forEach((item) => {
				const arr = item.split('=');
				sceneObj[arr[0]] = arr[1];
			})
			if (sceneObj.puid) {
				storeage.set('PUID.' + appid, sceneObj.puid);
			}
			if (sceneObj.dealer_uid) {
				storeage.set('DEALER_UID.' + appid, sceneObj.dealer_uid);
			}
			if (sceneObj.c) {
				const valid_time=Number(sceneObj.t)||0;
				storeage.set('CHANNELS_ID.' + appid, sceneObj.c,valid_time*60*60);
			}
		}
		// #endif
		// 初始化用户信息
		const UserStore=useUserStore();
		UserStore.initUserInfo();
		const token = options.query?.token;
		if (token) {
			UserStore.setUserInfo({ token })
			.then(() => {
				$api.User.userinfo(true);
			})
			.catch(() => {
			});
		}else{
			if (UserStore.hasLogin()) {
				$api.User.userinfo(true);
			}
		}
		// #ifdef APP
		uni.getNetworkType({
			success: (res:any)=>{
				if(res.networkType=='none'){
					uni.showToast({
						title: '网络连接失败，请检查网络',
						icon: 'none',
						duration: 2000
					});
					uni.onNetworkStatusChange((res:any)=>{
						if(res.isConnected){
							plus.runtime.restart();
						}
					});
				}
			}
		});
		// #endif
	})
	onShow(() => {
		if(watchShowGetWebConfig){
			$api.Config.webconfig();
		}
		console.log('App Show')
	})
	onHide(() => {
		console.log('App Hide')
	})
</script>

<style lang="scss">
	// #ifdef H5
	* {
		outline: none;
		box-sizing: border-box;
	}

	// #endif
	// #ifdef MP-WEIXIN
	view {
		outline: none;
		box-sizing: border-box;
	}
	// #endif
	/*每个页面公共css */
	@import "static/common.scss";
	@import "static/style.scss";
</style>